package com.nus.fellowmark.modules.assignment.service.impl;

import com.nus.fellowmark.dao.QuestionDao;
import com.nus.fellowmark.entity.QuestionEntity;
import com.nus.fellowmark.entity.RubricEntity;
import com.nus.fellowmark.modules.assignment.dto.QuestionDTO;
import com.nus.fellowmark.modules.assignment.dto.RubricDTO;
import com.nus.fellowmark.modules.assignment.service.QuestionService;
import com.nus.fellowmark.modules.assignment.service.RubricService;
import com.nus.fellowmark.modules.assignment.vo.QuestionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nus.fellowmark.common.utils.PageUtils;
import com.nus.fellowmark.common.utils.Query;
import org.springframework.transaction.annotation.Transactional;


@Service("questionService")
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, QuestionEntity> implements QuestionService {

    @Autowired
    QuestionDao questionDao;

    @Autowired
    RubricService rubricService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<QuestionEntity> page = this.page(
                new Query<QuestionEntity>().getPage(params),
                new QueryWrapper<QuestionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public QuestionVO addQuestion(QuestionDTO questionDTO) {
        QuestionEntity questionEntity = new QuestionEntity();
        BeanUtils.copyProperties(questionDTO, questionEntity);
        this.save(questionEntity);

        // set rubric
        RubricDTO rubricDTO = new RubricDTO();
        rubricDTO.setQuestionId(questionEntity.getId());
        BeanUtils.copyProperties(questionDTO, rubricDTO);
        rubricService.createRubric(rubricDTO);

        QuestionVO questionVO = new QuestionVO();
        BeanUtils.copyProperties(questionEntity,questionVO);
        BeanUtils.copyProperties(rubricDTO,questionVO);
        return questionVO;
    }

    @Override
    public void updateQuestion(QuestionDTO questionDTO) {
        QuestionEntity questionEntity = new QuestionEntity();
        BeanUtils.copyProperties(questionDTO, questionEntity);
        questionDao.updateById(questionEntity);
    }

    @Override
    public List<QuestionVO> listQuestions(Integer assignmentId) {
        QueryWrapper<QuestionEntity> queryWrapper = new QueryWrapper<QuestionEntity>().eq("assignment_id", assignmentId);
        List<QuestionEntity> questionEntities = questionDao.selectList(queryWrapper);

        List<QuestionVO> questionVOS = questionEntities.stream().map(questionEntity -> {
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(questionEntity, questionVO);
            RubricEntity rubricEntity = rubricService.getRubricsByQuestionId(questionEntity.getId());
            questionVO.setCriteria(rubricEntity.getCriteria());
            questionVO.setDescription(rubricEntity.getDescription());
            questionVO.setMinMark(rubricEntity.getMinMark());
            questionVO.setMaxMark(rubricEntity.getMaxMark());
            return questionVO;
        }).collect(Collectors.toList());

        return questionVOS;
    }

}
